package com.definesys.smecrailtransit.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.definesys.smecrailtransit.MainActivity;
import com.definesys.smecrailtransit.SmecApplication;
import com.definesys.smecrailtransit.receiver.receiverBean.JPushExtras;
import com.definesys.smecrailtransit.receiver.receiverBean.Push;
import com.definesys.smecrailtransit.service.Notification.NotificationService;
import com.definesys.smecrailtransit.utils.CommonUtils;
import com.google.gson.Gson;
import com.hwangjr.rxbus.SmecRxBus;

import cn.jpush.android.api.JPushInterface;

import static com.definesys.smecrailtransit.service.Notification.NotificationService.JPUSH_UPLOAD;

/**
 * Created by xupeizuo on 2017/7/20.
 */

public class JPushReceiver extends BroadcastReceiver{

    private static final String TAG=JPushReceiver.class.getSimpleName();
    public static final String JPUSH_TYPE_REPORT="FAULT_REPORT_SHOW";//站点详情推送
    public static final String JPUSH_TYPE_REAL="FAULT_MSG_SHOW";//实时状态推送
    public static final String JPUSH_TYPE_STATISTICS="FAULT_MONTH_STATISTICS";//故障统计-每月故障电梯数量推送
    public static final String JPUSH_TYPE_RUNTIME="ABNORMAL_RUNTIME_MONTH";//运行时长-每月非正常状态电梯数量推送
    public static final String JPUSH_TYPT_LINE_REPORT = "LAST_DAY_STATISTICS";//线路简报-每日前一日线路简报推送
    public static final String ACTIVITY_OPENDED="ACTIVITY_OPENDED";

    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle bundle = intent.getExtras();
        JPushExtras jPushExtras=null;
        if(JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())){
            String notificationExtras=bundle.getString(JPushInterface.EXTRA_EXTRA);
            jPushExtras=_jsonToObject(notificationExtras);
            Log.e(TAG,jPushExtras.toString()+"********received");
            if(!SmecApplication.isServiceWork(context,"com.definesys.smecrailtransit.service.Notification.NotificationService")){
                Intent service = new Intent(context, NotificationService.class);
                context.startService(service);
            }
            SmecRxBus.get().post(JPUSH_UPLOAD,jPushExtras.getUuid());
        }
        if(JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())){

            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );

            String notificationExtras=bundle.getString(JPushInterface.EXTRA_EXTRA);
            jPushExtras=_jsonToObject(notificationExtras);
            Log.e(TAG,jPushExtras.toString()+"************");
            if(jPushExtras == null){
                return;
            }
            if(CommonUtils.isForeground(context)){
                SmecRxBus.get().post(ACTIVITY_OPENDED,jPushExtras);
            }else {
                Intent intent1=new Intent(context,MainActivity.class);
                intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );

                String[] param = new String[3];
                param[0] = jPushExtras.getFunName();
                param[1] = jPushExtras.getFrom();
                param[2] = jPushExtras.getTo();
                intent1.putExtra("JPUSH_TYPE",param);
                context.startActivity(intent1);
            }
        }
    }

    /**
     * json 转化为 object
     * @param jsonString
     * @return
     */
    private JPushExtras _jsonToObject(String jsonString){
        Gson gson=new Gson();

        try {
            Push push=gson.fromJson(jsonString,Push.class);
            String extras=push.getEXTRA();
            JPushExtras jpushExtras=gson.fromJson(extras,JPushExtras.class);
            return jpushExtras;
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }
}
